package config

func init() {
	LoadConfigService()
}

var ServiceConfig *serviceConfig

type serviceConfig struct {
	Upstreams Upstreams
	Services  Services
}

type Upstreams map[string]Upstream

type Upstream struct {
	Routekey     string    `yaml:"routekey"`
	RemovePrefix bool      `yaml:"remove_prefix"`
	Queue        string    `yaml:"queue"`
	TimeOut      int       `yaml:"timeout"`
	Services     []string  `yaml:"services"`
	Weights      []float32 `yaml:"weights"`
}

type Services []Service

type Service struct {
	Name          string `yaml:"name"`
	Upstream      string `yaml:"upstream"`
	TokenCapacity int    `yaml:"tokencapacity"`
	TimeOut       int    `yaml:"timeout"`
}

func LoadConfigService() {
	//todo 不直接从数据库中读取配置
	//admin产生修改后生成新config文件
	//网关定时轮询接口获取配置
	ServiceCacheTmp := &serviceConfig{}
	upstreams := App.Upstreams
	if upstreams == nil {
		return
	}
	ServiceCacheTmp.Upstreams = App.Upstreams
	ServiceCacheTmp.Services = App.Services
	ServiceConfig = ServiceCacheTmp
}
